package com.mdh.runerrands.serviceImpl;

import com.mdh.runerrands.dao.*;
import com.mdh.runerrands.pojo.*;
import com.mdh.runerrands.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private AddressDao addressDao;

    @Resource
    private BaseUserDao baseUserDao;

    @Resource
    private ProductDao productDao;

    @Resource
     private  OrderLineDao orderLineDao;

    @Resource
    private AccountEmpDao accountEmpDao;


    @Override
    public List<Order> findAll() {
        return orderDao.selectAll();
    }

    @Override
    public boolean deleteById(int id) {
        return orderDao.deleteById(id);
    }

    @Override
    public OrderDetail findById(int id) {
        OrderDetail orderDetail = new OrderDetail();
        Order order = orderDao.selectById(id);
        orderDetail.setId(order.getId());
        orderDetail.setOrderTime(order.getOrderTime());
        orderDetail.setAddressId(order.getAddressId());
        orderDetail.setCustomerId(order.getCustomerId());
        orderDetail.setRemark(order.getRemark());
        orderDetail.setTotal(order.getTotal());
        orderDetail.setWaiterId(order.getWaiterId());
        orderDetail.setStatus(order.getStatus());
        orderDetail.setAddressId2(order.getAddressId2());
        Address address = addressDao.selectById(order.getAddressId());
        Address address1 = addressDao.selectById(order.getAddressId2());
        orderDetail.setAddress2(address1);
        orderDetail.setAddress(address);
        orderDetail.setNote(order.getNote());
        orderDetail.setCustomer(baseUserDao.selectById(order.getCustomerId()));
        orderDetail.setWaiter(baseUserDao.selectById(order.getWaiterId()));
        List<OrderLine> orderLines = orderLineDao.selectByOrderId(order.getId());
        List<OrderLineDetail> list = new ArrayList();
        for (OrderLine orderLine :orderLines){
            OrderLineDetail orderLineDetail = new OrderLineDetail();
            orderLineDetail.setId(orderLine.getId());
            orderLineDetail.setNumber(orderLine.getNumber());
            orderLineDetail.setOrderId(orderLine.getOrderId());
            orderLineDetail.setPrice(orderLine.getPrice());
            orderLineDetail.setProductId(orderLine.getProductId());
            orderLineDetail.setProduct(productDao.selectById(orderLine.getProductId()));
            list.add(orderLineDetail);
        }

        orderDetail.setOrderLines(list);
        return orderDetail;
    }

    @Override
    public List<OrderDetail> query(Integer customerId, Integer waiterId, String status) {
        List<Order> query = orderDao.query(customerId, waiterId, status);
        List<OrderDetail> list = new ArrayList();
        for (Order order :query){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(order.getId());
            orderDetail.setOrderTime(order.getOrderTime());
            orderDetail.setAddressId(order.getAddressId());
            orderDetail.setCustomerId(order.getCustomerId());
            orderDetail.setRemark(order.getRemark());
            orderDetail.setTotal(order.getTotal());
            orderDetail.setWaiterId(order.getWaiterId());
            orderDetail.setStatus(order.getStatus());
            Address address = addressDao.selectById(order.getAddressId());
            orderDetail.setAddress(address);
            orderDetail.setCustomer(baseUserDao.selectById(order.getCustomerId()));
            orderDetail.setWaiter(baseUserDao.selectById(order.getWaiterId()));
            orderDetail.setAddressId2(order.getAddressId2());
            orderDetail.setAddress2(addressDao.selectById(order.getAddressId2()));
            orderDetail.setNote(order.getNote());
            List<OrderLine> orderLines = orderLineDao.selectByOrderId(order.getId());
            List<OrderLineDetail> listOrderLine = new ArrayList();
            for (OrderLine orderLine :orderLines){
                OrderLineDetail orderLineDetail = new OrderLineDetail();
                orderLineDetail.setId(orderLine.getId());
                orderLineDetail.setNumber(orderLine.getNumber());
                orderLineDetail.setOrderId(orderLine.getOrderId());
                orderLineDetail.setPrice(orderLine.getPrice());
                orderLineDetail.setProductId(orderLine.getProductId());
                orderLineDetail.setProduct(productDao.selectById(orderLine.getProductId()));
                listOrderLine.add(orderLineDetail);
            }
            orderDetail.setOrderLines(listOrderLine);
            list.add(orderDetail);
        }
        return list;
    }

    @Override
    public boolean update(Order order) {
        return orderDao.update(order);
    }

    @Override
    public boolean batchDelete(int... ids) {
        return orderDao.batchDelete(ids);
    }

    @Override
    public boolean sendOrder(int id, int waiterId) {
        return orderDao.updateByIdAndWaiterId(id,waiterId);
    }

    @Override
    public List<OrderDetail> queryPage(Integer page, Integer pageSize, Long orderTime, Integer total, String status, Integer customerId, Integer waiterId,Integer orderId) {
        page = (page-1)*pageSize;
        List<Order> orderList = orderDao.queryPage(page, pageSize, orderTime, total, status, customerId, waiterId,orderId);
        List<OrderDetail> list = new ArrayList();
        if (orderList.size()==0){
            return null;
        }
        for (Order order :orderList){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(order.getId());
            orderDetail.setOrderTime(order.getOrderTime());
            orderDetail.setAddressId(order.getAddressId());
            orderDetail.setCustomerId(order.getCustomerId());
            orderDetail.setRemark(order.getRemark());
            orderDetail.setTotal(order.getTotal());
            orderDetail.setWaiterId(order.getWaiterId());
            orderDetail.setStatus(order.getStatus());
            Address address = addressDao.selectById(order.getAddressId());
            orderDetail.setAddress(address);
            orderDetail.setCustomer(baseUserDao.selectById(order.getCustomerId()));
            orderDetail.setWaiter(baseUserDao.selectById(order.getWaiterId()));
            List<OrderLine> orderLines = orderLineDao.selectByOrderId(order.getId());
            List<OrderLineDetail> listOrderLine = new ArrayList();
            for (OrderLine orderLine :orderLines){
                OrderLineDetail orderLineDetail = new OrderLineDetail();
                orderLineDetail.setId(orderLine.getId());
                orderLineDetail.setNumber(orderLine.getNumber());
                orderLineDetail.setOrderId(orderLine.getOrderId());
                orderLineDetail.setPrice(orderLine.getPrice());
                orderLineDetail.setProductId(orderLine.getProductId());
                orderLineDetail.setProduct(productDao.selectById(orderLine.getProductId()));
                listOrderLine.add(orderLineDetail);
            }
            orderDetail.setOrderLines(listOrderLine);
            list.add(orderDetail);
        }
        return list;
    }

    @Override
    public int selectCount(Long orderTime, Integer total, String status, Integer customerId, Integer waiterId,Integer orderId) {
        return orderDao.selectCount(orderTime,total,status,customerId,waiterId ,orderId);
    }

    @Override
    public boolean takeOrder(int orderId) {
        String status = "待服务";
        return orderDao.updateTakeOrder(orderId,status);
    }

    @Override
    public boolean serviceCompleteOrder(int orderId) {
        String status = "待确认";
        return orderDao.updateTakeOrder(orderId,status);
    }

    @Override
    public boolean confirmOrder(int orderId) throws Exception{
        String status = "已完成";
        boolean b = orderDao.updateTakeOrder(orderId, status);
        if (!b){
            return false;
        }
        Order order = orderDao.selectById(orderId);
        BaseUser waiter = baseUserDao.selectById(order.getWaiterId());
        BaseUser customer = baseUserDao.selectById(order.getCustomerId());
        AccountEmployee accountEmployee = new AccountEmployee();
        accountEmployee.setOrderId(orderId);
        accountEmployee.setDescription(customer.getUsername()+"所支付的费用");
        accountEmployee.setMoney(Integer.toString(order.getTotal()) );
        accountEmployee.setStatus(0);
        accountEmployee.setType("收益");
        accountEmployee.setTransactionTime(String.valueOf(System.currentTimeMillis()));
        accountEmployee.setUserId(waiter.getId());
        accountEmployee.setCustomerName(customer.getUsername());
        accountEmployee.setWaiterName(waiter.getUsername());
        boolean insert = accountEmpDao.insert(accountEmployee);
        if (!insert){
            return false;
        }
        return true;
    }

    @Override
    public boolean insert(Order order) {
        return orderDao.insert(order);
    }

    @Override
    public Order selectByTime(Long time) {
        return orderDao.selectByTime(time);
    }

    @Override
    public boolean updateRemark(int id, String remark) {
        return orderDao.updateRemark(id,remark);
    }

    @Override
    public boolean cancelOrder(Integer orderId) {
        return orderDao.updateStatus(orderId);
    }

    @Override
    public boolean updateStatus(Integer orderId, String status) {
        return orderDao.updateTakeOrder(orderId,status);
    }


}

